// 云函数入口文件
const cloud = require('wx-server-sdk');

cloud.init({
    env: 'dragondaily-nstam'
});

const db = cloud.database();

// 云函数入口函数
exports.main = async (event, context) => {
    const { openID, appID } = cloud.getWXContext(); // 这里获取到的 openId 和 appId 是可信的
    const { fromLabel, toLabel, messageContent, messageImageUrls } = event;
    const messageBoardCollection = db.collection('messageboard');

    const messageData = {
        fromLabel: fromLabel,
        toLabel: toLabel,
        messageContent: messageContent,
        messageImageUrls: messageImageUrls,
        fromOpenID: openID,
        fromAppID: appID,
        likeCount: 0,
        shareCount: 0
    };

    const addResult = await messageBoardCollection.add({
        data: messageData
    });

    if (!addResult._id) {
        return {
            status: 'fail',
            message: '发送失败'
        };
    }

    return {
        status: 'success'
    };
};
